﻿using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;

namespace Plasp.WebScoketServer.Web.WebSocketProvider
{
    public static class WebSocketExtension
    {
        /// <summary>
        /// 添加WebSocket服务
        /// </summary>
        /// <param name="services"></param>
        /// <returns></returns>
        public static IServiceCollection AddWebSocketManager(this IServiceCollection services) {
            services.AddScoped<ConnectionManager>();
            services.AddSingleton<WebSocketHandler>();

            return services;
        }

        /// <summary>
        /// 使用WebSocket中间件
        /// </summary>
        /// <param name="app"></param>
        /// <param name="path"></param>
        /// <param name="socketHandler"></param>
        /// <returns></returns>
        public static IApplicationBuilder MapSockets(this IApplicationBuilder app, PathString path,
            WebSocketHandler socketHandler) {
            return app.Map(path, (x) => x.UseMiddleware<WebSocketMiddleware>(socketHandler));
        }
    }
}
